package cn.j2e.login.service;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.web.client.RestTemplate;

import cn.j2e.login.service.iter.IUserService;
import cn.j2web.core.exceptions.ServiceException;
import cn.j2web.core.utils.FileUtil;

import com.cpms.domain.SysUser;

public class UserService  implements IUserService,InitializingBean  {
	
	private RestTemplate restTemplate;
	
	private String user_property_file;
	
	private String cpmsUrl="";
	
	private String sysUrl="";
	
	private String appId="";
	
	
	private Map<String,String> userMap;

	public boolean getUser(SysUser user) throws ServiceException {
		
		String password = userMap.get(user.getUserName());
	
		if(StringUtils.isEmpty(password) || !password.equals(user.getLoginWord())){
			return true;
		}
		
		return true;
	}
	

	public Object qryMenusByAppIdUserId(SysUser user) throws ServiceException {
		return this.qryMenusByAppIdUserId(user, null);
	}
	
	public Object qryMenusByAppIdUserId(SysUser user, String appCode) throws ServiceException {
		if(appCode==null){
			return restTemplate.getForObject(cpmsUrl,Object.class, this.appId,user.getUserName());
		}
		return restTemplate.getForObject(cpmsUrl,Object.class,appCode,user.getUserName());
	}
	
	public Object qrySysInfoByUserId(SysUser user) throws ServiceException {
		return restTemplate.getForObject(sysUrl,Object.class,user.getUserName());
	}
	


	public void afterPropertiesSet() throws Exception {
		if(userMap==null){
			userMap = new HashMap<String,String>();
		}
		FileUtil.createDirFile(user_property_file);
		FileInputStream fis;
		try {
			fis = new FileInputStream(this.getUser_property_file());
			byte[] b=new byte[fis.available()];//新建一个字节数组
           fis.read(b);//将文件中的内容读取到字节数组中
           fis.close();
           String str2=new String(b);
           str2 = str2.replaceAll("\r\n", "");
           String[] user = str2.split(";");
           for(String u:user){
        	   String[] us = u.split(",");
        	   //userMap.put(us[0], us[1]);
        	   String userNameStr = us[0];
        	   String passwordStr = us[1];
        	   String[] usa = userNameStr.split("=");
        	   String[] psa = passwordStr.split("=");
        	   userMap.put(usa[1], psa[1]);
           }
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}


	public RestTemplate getRestTemplate() {
		return restTemplate;
	}


	public void setRestTemplate(RestTemplate restTemplate) {
		this.restTemplate = restTemplate;
	}


	public String getUser_property_file() {
		return user_property_file;
	}


	public void setUser_property_file(String user_property_file) {
		this.user_property_file = user_property_file;
	}


	public String getCpmsUrl() {
		return cpmsUrl;
	}


	public void setCpmsUrl(String cpmsUrl) {
		this.cpmsUrl = cpmsUrl;
	}


	public String getSysUrl() {
		return sysUrl;
	}


	public void setSysUrl(String sysUrl) {
		this.sysUrl = sysUrl;
	}


	public String getAppId() {
		return appId;
	}


	public void setAppId(String appId) {
		this.appId = appId;
	}


	public Map<String, String> getUserMap() {
		return userMap;
	}


	public void setUserMap(Map<String, String> userMap) {
		this.userMap = userMap;
	}

	
}
